package com.example.ekupiandroid;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class Start extends Activity {
	Spinner spinner;
	Spinner spinner2;
	List<String> nizNazivMarketa;
	List<String> nizLokacijaPoslovnica;
	Context ctx;
	String idPoslovnice;
	String nazivMarketa;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		spinner = (Spinner) findViewById(R.id.spinner1);
		nizNazivMarketa = new ArrayList<String>();
		nizNazivMarketa.add("prvi");
			Thread t = null;
			try {
				t = new Thread (new Runnable() {
					@Override
					public void run() {
						String odgovor = null;
						try {
							WebServiceRequest wst = new WebServiceRequest(WebServiceRequest.GET_TASK);
							odgovor = wst.Execute("http://10.0.2.2:8080/eKupovina/webservices/marketi/svi");
							JSONObject json = new JSONObject(odgovor);
							JSONArray jArray = json.getJSONArray("market");
							for(int i=0;i<jArray.length();i++){
								JSONObject json_data = jArray.getJSONObject(i);
								final String nazivMarketa = json_data.getString("naziv");
								nizNazivMarketa.add(nazivMarketa);
							}					
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
			t.start();
			// Create an ArrayAdapter using the string array and a default spinner layout
		//	ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
			//        R.array.planets_array, android.R.layout.simple_spinner_item);
			//nizNazivMarketa.remove(0);
			ctx = this;
			ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, nizNazivMarketa);
			// Specify the layout to use when the list of choices appears
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			// Apply the adapter to the spinner
			spinner.setAdapter(adapter);
			
			
			
			spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
		        public void onItemSelected(AdapterView<?> arg0, View v, int i, long lng) 
		        {
					// final EditText et = (EditText) findViewById(R.id.editText1);
					Thread t = null;
					final int index = i;
					spinner2 = (Spinner) findViewById(R.id.spinner2);
					nizLokacijaPoslovnica = new ArrayList<String>();
					nizLokacijaPoslovnica.add("prva");
					nazivMarketa = nizNazivMarketa.get(index);
					try {
						t = new Thread (new Runnable() {
							@Override
							public void run() {
								String odgovor = null;
								WebServiceRequest wst = new WebServiceRequest(WebServiceRequest.GET_TASK);
								if(nizNazivMarketa.get(index)!="prvi"){
									odgovor = wst.Execute("http://10.0.2.2:8080/eKupovina/webservices/poslovnice/sve/" + nizNazivMarketa.get(index));
								}
								//odgovor = wst.Execute("http://10.0.2.2:8080/eKupovina/webservices/poslovnice/sve/Drvopromet");
								
							/**	
								runOnUiThread(new Runnable() {
								     @Override
								     public void run() {
								    	 et.setText(URL2);

								    }
								});*/
								
								try{
									if(odgovor != null){
										JSONObject json = new JSONObject(odgovor);
										JSONArray jArray = json.getJSONArray("poslovnica");
										for(int i=0; i<jArray.length(); i++){
											JSONObject json_data = jArray.getJSONObject(i);
											final String lokacija = json_data.getString("lokacija");
											nizLokacijaPoslovnica.add(lokacija);
										}										
									}
								}catch(Exception jex){
									
								}
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}
					t.start();
						ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(ctx, android.R.layout.simple_spinner_item, nizLokacijaPoslovnica);
						// Specify the layout to use when the list of choices appears
						adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
						// Apply the adapter to the spinner
						spinner2.setAdapter(adapter2);
						spinner2.setOnItemSelectedListener(itemSelectedListener);
					
		        }
				@Override
			    public void onNothingSelected(AdapterView<?> arg0) {
			        // TODO Auto-generated method stub

			    }
		    });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.start, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	       // case R.id.help:
	        	//Intent registracija = new Intent(getBaseContext(), RegistracijaActivity.class);
				//startActivity(registracija);
	           // return true;
	        case R.id.action_settings:
	           // settings
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	
	public OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener(){
		@Override
        public void onItemSelected(AdapterView<?> arg0, View v, int i, long lng) 
        {
			final int index = i;
			Thread t = null;
			try {
				t = new Thread (new Runnable() {
					@Override
					public void run() {
						String odgovor = null;
						try {
							if(nizLokacijaPoslovnica.get(index) != "prva"){
								WebServiceRequest wst = new WebServiceRequest(WebServiceRequest.GET_TASK);
								odgovor = wst.Execute("http://10.0.2.2:8080/eKupovina/webservices/poslovnice/posl/" + nizLokacijaPoslovnica.get(index).replaceAll(" ", "%20"));
								final String oo = odgovor;
								JSONObject json = new JSONObject(odgovor);
								idPoslovnice = json.getString("idPoslovnica");
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
			t.start();
        }
	    @Override
	    public void onNothingSelected(AdapterView<?> arg0) {
	    	
	    }
	};


	public void ZapocniKupovinu(View view){
		Intent start = new Intent(getBaseContext(), KreiranjeKorpe.class);
		start.putExtra("market", nazivMarketa);
		start.putExtra("IdPoslovnice", idPoslovnice);
		start.putExtra("korisnik", getIntent().getExtras().getString("korisnik"));
		startActivity(start);
	}
}
